#include "assign.h"
#include "AEEPTLayer.h"
#include "kmglang_en.h"
#include "dfon_shared_if_contacts.h"
#include "AEEConfig.h"
#include "dfon_comdef.h"

struct AssignCtx
{
	NodeFile node;
	IPTLayer_MenuList *menuAs;
	IPTLayer_MenuList *menuFor;
	uint16 selectedAsId;
};

MENUITEM ringerForTable[] = 
{
	{IDS_ASSIGN_FOR, 0, 0},
	{IDS_CONTACT_RINGER_VMU, 0, 0},
	{IDS_GROUP_RINGER_VMU, 0, 0},
	{IDS_ALL_CONTACTS_RINGER_VMU, 0, 0},
	{0,0,0}
};

MENUITEM ringerAsTable[] = 
{
	{IDS_RINGER_ASSIGN_AS, 0, 0},
	{IDS_RINGTONE, 0, 0},
	{IDS_TXT_TONE, 0, 0},
	{0,0,0}
};

MENUITEM imageForTable[] = 
{
	{IDS_ASSIGN_FOR, 0, 0},
	{IDS_CONTACT_RINGER_VMU, 0, 0},
	{IDS_GROUP_RINGER_VMU, 0, 0},
	{0,0,0}
};

MENUITEM imageAsTable[] = 
{
	{IDS_PICTURE_ASSIGN_AS, 0, 0},
	{IDS_PICTUREIDS_VMU, 0, 0},
	{IDS_WALLPAPER, 0, 0},
	{IDS_SCREENSAVER, 0, 0},
	{0,0,0}
};

static void GenerateMenu(MGallery *me, IPTLayer_MenuList **list, MENUITEM* table, PFNCLOSEHANDLER handler)
{
	if(*list)
		IPTLAYER_MenuListDestroy(me->ptl, *list);

	IPTLAYER_MenuListGenerate(me->ptl, list, table, me->resFileName, handler);
	IPTLAYER_MenuListSoftKeyAdd(me->ptl, *list, me->resFileName, IDS_SELECT, IDS_VMU_BACK);
	IPTLAYER_MenuListShow(me->ptl, *list, TRUE, IPTLAYER_NORMAL_BASE);
}


static void RingerForCloseHandler(void* data, int index)
{
	MGallery *me = data;

	if(index == PTLAYER_MENULIST_CLR || index == PTLAYER_MENULIST_SOFT2)
		IPTLAYER_MenuListShow(me->ptl, me->assign->menuFor, FALSE, IPTLAYER_NORMAL_BASE);
	else if(index >= 0 || index == PTLAYER_MENULIST_SOFT1)
	{
		AlertDesc alert = {NULL, IDS_SAVED, IDS_RINGTONE_ASSIGNED, 0};
		UTIL_HISTORY_DATA histData;
		int index = IPTLAYER_MenuListGetItemIndex(me->ptl, me->assign->menuFor);
		int itemID = IPTLAYER_MenuListGetItemID(me->ptl, me->assign->menuFor, (uint16)index);

		alert.resName = me->resFileName;

		switch(itemID)
		{
		case IDS_CONTACT_RINGER_VMU:
		case IDS_GROUP_RINGER_VMU:
			ZEROAT(&histData);
			histData.sender = AEECLSID_MG;
			histData.target = AEECLSID_DIFFON_CONTACTS;
			if(me->assign->selectedAsId == IDS_RINGTONE)
			{
				if(itemID == IDS_CONTACT_RINGER_VMU)
					histData.IFSymbol = KWC_CONTACTS_IF_RINGER_CALLER_ID;
				else
					histData.IFSymbol = KWC_GROUPS_IF_RINGER_CALLER_ID;
			}
			else
			{
				if(itemID == IDS_CONTACT_RINGER_VMU)
					histData.IFSymbol = KWC_CONTACTS_IF_MSG_ALERT;
				else
					histData.IFSymbol = KWC_GROUPS_IF_MSG_ALERT;
			}
			STRCPY((char*)histData.pBuf, me->assign->node.fi.szName);
			histData.nBufSize = STRLEN(me->assign->node.fi.szName) + 1;
			DFON_IUTILLIB_SetHistory(me->utils, DFON_HISTDATA_CONTACTS, &histData);
			ISHELL_StartApplet(me->shell, AEECLSID_DIFFON_CONTACTS);
			break;
		case IDS_ALL_CONTACTS_RINGER_VMU:
			//IPTLAYER_MakeAlert(me->ptl, &alert, NULL, NULL);
			break;
		}
		Assign_Destroy(me);
	}
}

static void RingerAsCloseHandler(void* data, int index)
{
	MGallery *me = data;
	if(index == PTLAYER_MENULIST_CLR || index == PTLAYER_MENULIST_SOFT2)
		IPTLAYER_MenuListShow(me->ptl, me->assign->menuAs, FALSE, IPTLAYER_NORMAL_BASE);
	else if(index >= 0 || index == PTLAYER_MENULIST_SOFT1)
	{
		int index = IPTLAYER_MenuListGetItemIndex(me->ptl, me->assign->menuAs);
		me->assign->selectedAsId = (uint16)IPTLAYER_MenuListGetItemID(me->ptl, me->assign->menuAs, (uint16)index);
		GenerateMenu(me, &me->assign->menuFor, ringerForTable, RingerForCloseHandler);
	}
}

static void ImageForCloseHandler(void* data, int index)
{
	MGallery *me = data;

	if(index == PTLAYER_MENULIST_CLR || index == PTLAYER_MENULIST_SOFT2)
		IPTLAYER_MenuListShow(me->ptl, me->assign->menuFor, FALSE, IPTLAYER_NORMAL_BASE);
}

static void ImageAsCloseHandler(void* data, int index)
{
	MGallery *me = data;

	if(index == PTLAYER_MENULIST_CLR || index == PTLAYER_MENULIST_SOFT2)
		IPTLAYER_MenuListShow(me->ptl, me->assign->menuAs, FALSE, IPTLAYER_NORMAL_BASE);

	if(index >= 0 || index == PTLAYER_MENULIST_SOFT1)
	{
		IConfig *config = NULL;
		int index = IPTLAYER_MenuListGetItemIndex(me->ptl, me->assign->menuAs);
		me->assign->selectedAsId = (uint16)IPTLAYER_MenuListGetItemID(me->ptl, me->assign->menuAs, (uint16)index);

		ISHELL_CreateInstance(me->shell, AEECLSID_CONFIG, (void**)&config);
				
		switch(me->assign->selectedAsId)
		{
		case IDS_PICTUREIDS_VMU:
			GenerateMenu(me, &me->assign->menuFor, imageForTable, ImageForCloseHandler);
			break;
		case IDS_WALLPAPER:
			break;
		case IDS_SCREENSAVER:
			break;
		}
		RELEASEIF(config);
		
	}

//	ICONFIG_SetItem(config, CFGI_UI_WALLPAPER, me->assign->node.fi, STRLEN(me->assign->node.fi)+1);
// 	ISHELL_SendEvent( pMe->a.m_pIShell, AEECLSID_DIFFON_IDLE,
// 		EVT_EA_NOTIFY_SETCHANGED, (uint16) DFON_SET_DISPLAY, DFON_REGLIB_DISPLAY_WALLPAPERS);
}

void Assign_Destroy(MGallery* me)
{
	if(me && me->assign)
	{
		if(me->assign->menuAs)
			IPTLAYER_MenuListDestroy(me->ptl, me->assign->menuAs);
		if(me->assign->menuFor)
			IPTLAYER_MenuListDestroy(me->ptl, me->assign->menuFor);
	}
	FREEIF(me->assign);
}

void Assign_Create(MGallery* me, NodeFile* node)
{
	if(me->assign)
		Assign_Destroy(me);

	me->assign = MALLOCREC(AssignCtx);
	me->assign->node = *node;

	if(node->image)
		GenerateMenu(me, &me->assign->menuAs, imageAsTable, ImageAsCloseHandler);
	else
		GenerateMenu(me, &me->assign->menuAs, ringerAsTable, RingerAsCloseHandler);
}